package com.tusuo.stds.ui.level;

import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.LevelRecordList;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.question.LevelRecordDetailActivity_;
import com.tusuo.stds.util.Utils;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.List;

import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;
import myandroid.library.util.ApplicationHelper;

@EActivity(R.layout.activity_level_record)
public class LevelRecordActivity extends BaseToolbarActivity<LevelRecordView, LevelRecordPresenter>
        implements LevelRecordView, Action, LevelRecordRvAdapter.OnItemClickListener {

    @ViewById(R.id.level_record_tv_no_list)
    TextView tvNoList;

    @ViewById(R.id.list_level_record)
    RefreshRecyclerView refreshRv;

    @StringRes(R.string.toolbar_title_level_record)
    String toolbarTitle;

    private LevelRecordRvAdapter rvAdapter;
    private LinearLayoutManager linearLayoutManager;

    @AfterViews
    void afterViews() {
        setupUI(this);
        rvAdapter = new LevelRecordRvAdapter(this);
        rvAdapter.setOnItemClickListener(this);
        refreshRv.setAdapter(rvAdapter);
        linearLayoutManager = new LinearLayoutManager(this);
        refreshRv.setLayoutManager(linearLayoutManager);
        refreshRv.setLoadMoreAction(this);
        refreshRv.setVisibility(View.VISIBLE);
        tvNoList.setVisibility(View.GONE);

        List<ToolbarOwner.MenuAction> menuActionList = new ArrayList<>();

        ToolbarOwner.Config config = new ToolbarOwner.Config(toolbarTitle, true, menuActionList);
        mToolbarOwner.setConfig(config);

        ApplicationHelper.getInstance().addActivity(this);
    }

    @NonNull
    @Override
    public LevelRecordPresenter createPresenter() {
        return new LevelRecordPresenter(this);
    }

    @Override
    public void updateRvData(List<LevelRecordList.LevelRecord> data) {
        rvAdapter.addAll(data);
    }

    @Override
    public void scrollRvToFirst() {
        linearLayoutManager.scrollToPosition(0);
    }

    @Override
    public void hintLoadMore() {
        refreshRv.showNoMore();
    }


    @Override
    public void showNoList() {
        refreshRv.setVisibility(View.GONE);
        tvNoList.setVisibility(View.VISIBLE);
    }

    @Override
    public void onAction() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                getPresenter().getRvData();
            }
        }, 1000);
    }

    @Override
    public void onItemClick(LevelRecordList.LevelRecord data) {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        Intent intent = new Intent();
        intent.setClass(this, LevelRecordDetailActivity_.class);
        intent.putExtra(LevelRecordList.LevelRecord.EXTRA_NAME, Parcels.wrap(data));
        startActivity(intent);
    }

//    @ItemClick(R.id.list_level_record)
//    void listItemClicked(LevelRecordList.LevelRecord levelRecord) {
//        if (Utils.UI.isFastDoubleClick()) {
//            return;
//        }
//
//        Intent intent = new Intent();
//        intent.setClass(this, LevelRecordDetailActivity_.class);
//        intent.putExtra(LevelRecordList.LevelRecord.EXTRA_NAME, Parcels.wrap(levelRecord));
//        startActivity(intent);
//    }
}
